
package com.oldphonedeals.controller;

import com.oldphonedeals.pojo.User;
import com.oldphonedeals.service.UserService;
import com.oldphonedeals.dto.UserUpdateDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public User register(@RequestBody User user) {
        return userService.register(user);
    }

    @PostMapping("/login")
    public String login(@RequestBody User user) {
        return userService.login(user);
    }

    @GetMapping("/me")
    public User getCurrentUser(Authentication authentication) {
        return userService.getCurrentUser(authentication.getName());
    }

    @PutMapping("/update")
    public User updateUser(@RequestBody UserUpdateDTO dto, Authentication authentication) {
        return userService.updateUser(authentication.getName(), dto);
    }
}
